/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DBLayer;

import ControlLayer.HelperMethods;
import DBLayer.Interfaces.IContainer;
import ModelLayer.Actor;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Moulde
 */
public class DBActor extends DatabaseAccess implements IContainer<Actor>
{
    DBEmployee DBemployee;
    DBSupplier DBsupplier;
    DBCustomer DBcustomer;

    public DBActor()
    {
        DBemployee = new DBEmployee();
        DBsupplier = new DBSupplier();
        DBcustomer = new DBCustomer();
    }

    public Actor getObject(int ID)
    {
        String query = "Select ActorID as ID, Actor.Name as Name, [Address], CityZipCode.ZipCode, City, Phonenumber, Country, Email From Actor, CityZipCode Where Actor.ZipCode = CityZipCode.ZipCode and ActorID = " + ID;
        GenericModelBinder<Actor> actorbinder = new GenericModelBinder<Actor>(Actor.class);

        try
        {
            Actor actor = actorbinder.BindModel(ExecuteQuery(query));
            attachModels(actor);
            return actor;
        }
        catch (SQLException ex)
        {
            Logger.getLogger(DBActor.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (InstantiationException ex)
        {
            Logger.getLogger(DBActor.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (IllegalAccessException ex)
        {
            Logger.getLogger(DBActor.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public ArrayList<Actor> getObjects()
    {
        String query = "Select ActorID as ID, Actor.Name as Name, [Address], CityZipCode.ZipCode, City, Phonenumber, Country, Email From Actor, CityZipCode Where Actor.ZipCode = CityZipCode.ZipCode";
        ResultSet actors = ExecuteQuery(query);
        ArrayList<Actor> list = new ArrayList<Actor>();

        GenericModelBinder<Actor> actorDb = new GenericModelBinder<Actor>(Actor.class);

        try
        {
            list = actorDb.BindModels(actors);

            for (int i = 0; i < list.size(); i++)
            {
                Actor actor = list.get(i);
                attachModels(actor);
            }

            return list;
        }
        catch (SQLException ex)
        {
            Logger.getLogger(DBActor.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (InstantiationException ex)
        {
            Logger.getLogger(DBActor.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (IllegalAccessException ex)
        {
            Logger.getLogger(DBActor.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public ArrayList<Actor> getObjectsByRole(String roleName)
    {
        String query = "Select Actor.ActorID as ID, Actor.Name as Name, [Address], CityZipCode.ZipCode, City, Phonenumber, Country, Email From Actor, CityZipCode, [Role], RoleName Where Actor.ZipCode = CityZipCode.ZipCode and [Role].ActorID = Actor.ActorID and Role.RoleNameID = RoleName.RoleNameID and RoleName.Name = '" + roleName + "'";
        ResultSet actors = ExecuteQuery(query);
        ArrayList<Actor> list = new ArrayList<Actor>();

        GenericModelBinder<Actor> actorDb = new GenericModelBinder<Actor>(Actor.class);

        try
        {
            list = actorDb.BindModels(actors);

            for (int i = 0; i < list.size(); i++)
            {
                Actor actor = list.get(i);
                attachModels(actor);
            }

            return list;
        }
        catch (SQLException ex)
        {
            Logger.getLogger(DBActor.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (InstantiationException ex)
        {
            Logger.getLogger(DBActor.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (IllegalAccessException ex)
        {
            Logger.getLogger(DBActor.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public Actor attachModels(Actor actor)
    {
        int actorID = actor.getActorID();
        actor.addRole(DBemployee.getEmployeeByActorID(actorID));
        actor.addRole(DBcustomer.getCustomerByActorID(actorID));
        actor.addRole(DBsupplier.getSupplierByActorID(actorID));
        return actor;
    }

    public Actor validateEmployeeLogin(String username, String password)
    {
        try
        {
            HelperMethods helper = new HelperMethods();
            String query = "Select Actor.ActorID From Actor Join Role on Role.ActorID = Actor.ActorID Join Employee on Role.RoleID = Employee.RoleID Where Employee.Username = '" + username + "' and Employee.Password = '" + helper.MD5Hash(password) + "'";
            Statement getStatement = dbConnection.createStatement();
            ResultSet results = getStatement.executeQuery(query);

            if (results.next())
            {
                int actorID = results.getInt(1);
                Actor actor = getObject(actorID);
                return actor;
            }
        }
        catch (SQLException ex)
        {
            Logger.getLogger(DBActor.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return null;
    }

    public HashMap<Integer, String> GetZipCodes()
    {
        try
        {
            String query = "select * from CityZipCode";
            Statement getStatement = dbConnection.createStatement();
            ResultSet results = getStatement.executeQuery(query);

            HashMap<Integer, String> zipCodes = new HashMap<Integer, String>();

            while (results.next())
            {
                zipCodes.put(results.getInt(1), results.getString(2));
            }

            return zipCodes;
        }
        catch (SQLException ex)
        {
            Logger.getLogger(DBActor.class.getName()).log(Level.SEVERE, null, ex);
        }

        return new HashMap<Integer, String>();
    }

    public boolean SaveObject(Actor object)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean DeleteObject(Actor object)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean UpdateObject(Actor object)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
